{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PHydro-cover-small.png\">\n",
    "*This is the Jupyter notebook version of the [Python in Hydrology](http://www.greenteapress.com/pythonhydro/pythonhydro.html) by Sat Kumar Tomer.*\n",
    "*Source code is available at [code.google.com](https://code.google.com/archive/p/python-in-hydrology/source).*\n",
    "\n",
    "*The book is available under the [GNU Free Documentation License](http://www.gnu.org/copyleft/fdl.html). If you have comments, corrections or suggestions, please send email to satkumartomer@gmail.com.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [The First Program](01.07-The-First-Program.ipynb) | [Contents](Index.ipynb) | [Data Types](02.01-Data-Types.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### [2. A Bit of Python](02.00-A-Bit-of-Python.ipynb)\n",
    "- [Data Types](02.01-Data-Types.ipynb)\n",
    "- [Data Structures](02.02-Data-Structures.ipynb)\n",
    "- [Choosing the Name of Variable](02.03-Choosing-the-Name-of-Variable.ipynb)\n",
    "- [Operators and Operands](02.04-Operators-and-Operands.ipynb)\n",
    "- [Expressions](02.05-Expressions.ipynb)\n",
    "- [Control Flows](02.06-Control-Flows.ipynb)\n",
    "- [Functions](02.07-Functions.ipynb)\n",
    "- [Plotting](02.08-Plotting.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二章\n",
    "# 简明的Python\n",
    "\n",
    "在将Python应用到水文学之前，需要编写许多代码、操作数组等。最好学一些Python的基础知识，例如数据类型、循环(多次执行相同的任务)和编写函数等。首先要了解的是数据类型。\n",
    "\n",
    "## 2.1 数据类型\n",
    "\n",
    "有两种主要地的数据类型：数字和字符串。类型函数返回数据类型。\n",
    "\n",
    "### 2.1.1 数字\n",
    "\n",
    "在Python中有三种类型的数字：整数、浮点数和复数。整数是数组(向量，矩阵)索引、计数所需要的等。在Python中，不需要事先定义变量类型，在需要的时候，甚至可以在程序的后面修改数据类型。\n",
    "\n",
    "```\n",
    ">>> a = 5\n",
    ">>> type(a)\n",
    "<type 'int'>\n",
    "```\n",
    "这意味着，数据类型是整数。程序中没有`>>>`的行表示Python的输出。另一种最常用的数据类型是浮点数。大多数水文变量都属于这一类数据类型。\n",
    "\n",
    "```\n",
    ">>> b = 7.8\n",
    ">>> type(b)\n",
    "<type 'float'>\n",
    "```\n",
    "这意味着数据类型是浮点数。另一种数据类型是复数，这在日常的水文生活中并不经常需要。\n",
    "\n",
    "```\n",
    ">>> c = 5 + 2j\n",
    ">>> type(c)\n",
    "<type 'complex'>\n",
    "```\n",
    "`c`表示复数数据类型。\n",
    "\n",
    "###  2.1.2 字符串\n",
    "\n",
    "字符串是字符序列。有三种指定字符串的方式。\n",
    "\n",
    "**单引号:**在Python中写在单引号里面的被称为字符串。\n",
    "\n",
    "**双引号:**双引号也用于定义字符串。如果单引号就能够定义字符串，为什么还需要双引号呢？让我们尝试使用单引号写`‘What's your name?’`。\n",
    "\n",
    "```\n",
    ">>> foo = 'what's your name?'\n",
    "File \"<stdin>\", line 1\n",
    "   foo ='what's your name?'\n",
    "          ^\n",
    "SyntaxError: invalid syntax\n",
    "```\n",
    "\n",
    "这里产生了语法错误。让我们尝试使用双引号。\n",
    "\n",
    "```\n",
    ">>> foo = \"what's your name?\"\n",
    ">>> print(foo)\n",
    "what's your name?\n",
    "```\n",
    "\n",
    "双引号是定义包含单引号字符串的一种简单方式。然而，相同的任务也可以采用单引号做到。相同的字符串只需要在`'`之前添加`\\`就可以使用单引号编写了。\n",
    "\n",
    "```\n",
    ">>> foo = 'what\\'s your name?'\n",
    ">>> print(foo)\n",
    "what's your name?\n",
    "```\n",
    "\n",
    "**三引号:**当字符串空格超过一行时，三引号是定义它们的最好方式。多行字符串可以通过在单引号或双引号中指定换行符`\\n`来实现，而三引号则进行了简化。三引号在其他事上(为函数写帮助内容)也有很大用处，你将在本书后面的内容中读到。\n",
    "\n",
    "```\n",
    ">>> foo = \"\"\"My name is Sat Kumar.\n",
    "... I am in PhD \"\"\"\n",
    ">>> print foo\n",
    "My name is Sat Kumar.\n",
    "I am in PhD\n",
    "```\n",
    "\n",
    "## 2.2 数据结构\n",
    "\n",
    "数据结构能够在其中包含多个数据。Python中有四种内嵌的数据结构：列表(list)、元组(tuple)、字典(dictionary)和集合(set)。除了这些内嵌的数据结构，你可以像`numpy.array`一样用`numpy`定义你自己的数据类型，这是非常有用的。我并不觉得有必要在水文学中使用集合，所以我在这里略过集合，如果你有兴趣，你可以从其它渠道学习。\n",
    "\n",
    "## 2.2.1 列表\n",
    "\n",
    "列表是项(值)的序列。其中的项可以属于任何数据类型，并且可以在同一个列表中包含不同的数据类型。\n",
    "\n",
    "```\n",
    ">>> a = ['Ram','Sita','Bangalore','Delhi']\n",
    ">>> b = [25, 256, 2656, 0]\n",
    ">>> c = [25,'Bangalore']\n",
    "```\n",
    "\n",
    "列表中的项使用索引访问。变量a和b容纳类似数据类型的项，而c容纳不同数据类型的项。在Python中，索引从0开始。因此，要得到第一和第三项，索引应该是0和2。 \n",
    "\n",
    "```\n",
    ">>> a = ['Ram','Sita','Bangalore','Delhi']\n",
    ">>> print a[0]\n",
    "Ram\n",
    ">>> print a[2]\n",
    "Bangalore\n",
    "```\n",
    "\n",
    "Python中负数索引也是允许的。列表中最后一项的索引是`-1`,类似地，倒数第二项的索引为`-2`等。\n",
    "\n",
    "```\n",
    ">>> a = ['Ram','Sita','Bangalore','Delhi']\n",
    ">>> print a[-1]\n",
    "Delhi\n",
    "```\n",
    "\n",
    "同样，可以使用索引`-2`访问列表中倒数第二项。\n",
    "\n",
    "\n",
    "### 2.2.3 字典\n",
    "\n",
    "在列表中，索引只是整数。字典具有将任何数据类型作为索引的能力。当索引名称等时，字典的这一特性使它非常适合。例如，在水文中，每个站点都有字段站点的名称及其相应的变量。让我们先使用列表检索变量的值，然后使用字典。我们可以使用一个列表来存储站点的名称，用另一个列表存储变量的名称。首先，我们需要查找站点的索引，然后使用这些索引从变量列表中访问变量。\n",
    "\n",
    "```\n",
    ">>> a = ['Delhi','Bangalore','Kolkata']\n",
    ">>> rainfall = [0, 5, 10]\n",
    ">>> print(rainfall[a.index('Bangalore')])\n",
    "5\n",
    "```\n",
    "现在，让我们使用字典，\n",
    "\n",
    "```\n",
    ">>> rainfall = {'Delhi':0,'Bangalore':5,'Kolkata':10}\n",
    ">>> rainfall['Bangalore']\n",
    "5\n",
    "```\n",
    "同样的事情也可以在一行中使用，但是字典提供了一个整洁的方法来完成这个任务。\n",
    "\n",
    "### 2.2.3 元组\n",
    "\n",
    "元组是一些列的值，类似于列表，除了元组是不可变(它们的值不能被修改)外。\n",
    "\n",
    "```\n",
    ">>> foo = 5,15,18\n",
    ">>> foo[2]\n",
    "5\n",
    ">>> foo[1] = 10\n",
    "Traceback (most recent call last):\n",
    "  File \"<stdin>\", line 1, in <module>\n",
    "TypeError:'tuple' object does not support item assignment\n",
    "```\n",
    "\n",
    "当尝试修改元组中的项时，Python会出错误。元组在需要指定某些常量，并确保这些变量保持不变时很有用。元组的不可变属性确保在程序执行过程中常量的值不会改变。\n",
    "\n",
    "只有一个项的元组是通过在之后使用`,`来定义的，例如：\n",
    "\n",
    "```\n",
    ">>> foo = 5\n",
    ">>> type(foo)\n",
    "<type'int'>\n",
    ">>> foo = 5,\n",
    ">>> type(foo)\n",
    "<type'tuple'>\n",
    "```\n",
    "\n",
    "你可能注意到，如果不使用冒号(,)，Python就不会把它当作元组。\n",
    "\n",
    "### 2.2.1 Numpy.array\n",
    "\n",
    "NumericalPython (NumPy)是一个主要用C语言写的库/程序包，但为Python提供了应用程序接口(API)。该库提供的`numpy.array`数据类型在数组的数值计算中非常有用，这是我们绝大多数时候最常处理的数据类型。该库并非Python标准库的一部分，在使用之前，需要先在系统中安装Numpy。我们可以使用下面的命令检查Numpy是否已安装在我们的系统中。\n",
    "\n",
    "```\n",
    "$ python -c'import numpy'\n",
    "```\n",
    "如果这个指令并没有给出输出(没有错误)，则表明Numpy已被安装了。如果在系统中Numpy没有被安装，则你讲会看到如下信息(错误)：\n",
    "\n",
    "```\n",
    "$ python -c'import numpy'\n",
    "Traceback (most recent call last):\n",
    "File \"<string>\", line 1, in <module>\n",
    "ImportError: No module named numpy\n",
    "```\n",
    "\n",
    "这意味着，在系统中numpy没有被安装。你可以按照1.3节提供的步骤进行安装。`python -c'import numpy' `是一种运行简单代码而不xxxpython的方式，当你希望做某些小而快的事时非常有用。当你想要检查某些包是否在系统中已被安装也是非常有用的。\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
